package com.cqhfan.common.autoconfigure.mvc.advice;

import com.cqhfan.common.domain.Result;
import com.cqhfan.common.exceptions.BizException;
import com.cqhfan.common.exceptions.DbException;
import com.cqhfan.common.exceptions.UnLoginException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 眭传洪
 * @create 2023/12/24 0:51
 */
@Slf4j
@RestControllerAdvice
public class CommonExceptionHandler {

    @ExceptionHandler(BizException.class)
    public Result<Object> handleBizException(RuntimeException exception) {
        String message = exception.getMessage();
        log.error(message);
        return Result.fail(message);
    }

    @ExceptionHandler(DbException.class)
    public Result<Object> handleDbException(RuntimeException exception) {
        String message = exception.getMessage();
        log.error(message);
        return Result.fail(message);
    }

    @ExceptionHandler(UnLoginException.class)
    public Result<Object> handleUnLoginException(RuntimeException exception) {
        log.info("还未登录");
        Result<Object> result = new Result<>();
        result.setCode(HttpStatus.UNAUTHORIZED.value());
        result.setData(null);
        result.setMsg("还未登录");
        return result;
    }

}
